<?php

/**
 * Flickr Photosets - Collections Page
 * Displays a list of the user's collections.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets and Flickr
*/
$fp = new FP_Canvas();

// If the user has disabled collections
if (!$fp->config['enable_gallery_collections']) {
	if ($fp->config['enable_gallery_photosets']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/sets/');
	}
	else if ($fp->config['enable_gallery_photostream']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/');
	}
	else if ($fp->config['enable_gallery_geotagged']) {
		$fp->facebook->redirect($fp->canvas_url.'/photos/'.$fp->uid.'/geo/');
	}
	else {
		$fp->error(FP_Error::$collections_disabled);
	}
}

// Get the collections and count
$collections = $fp->flickr->call('flickr.collections.getTree', array('auth_token' => $fp->config['flickr_token'], 'user_id' => $fp->nsid));
$collections = $collections['collections'];
$collection_count = count($collections['collection']);

// Slice the array into the collections that will be displayed on this page only
$collections['collection'] = array_slice($collections['collection'], (($fp->page - 1) * $fp->sets_per_page), $fp->sets_per_page);

// Calculate the number of pages we will have
$page_count = ceil($collection_count / $fp->sets_per_page);

?>

<fb:title><?=$fp->get_name_fbml(true)?> Collections</fb:title>
<?=$fp->get_stylesheet('collections')?>

<?=$fp->get_header('collections')?>

<?php if ($collection_count < 1) {
	$fp->error(FP_Error::$no_collections);
} else { ?>
	<div id="photo_header" class="clearfix">
		<div class="summary">
			<?=$collection_count?> <fb:intl>Collections</fb:intl>
			<span class="pipe">|</span>
			<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>collections/">View collections on Flickr</a>
		</div>
		<?=Pager::getPager($fp->page, $page_count, $fp->canvas_url.'/photos/'.$fp->uid.'/collections/')?>
	</div>
	
	<div class="collection_grid">
		<table>
			<tr>
				<?php $i = 0;
				foreach ($collections['collection'] as $collection): ?>
					<td>
						<div class="collection">
							<div class="image">
								<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/collections/<?=$collection['id']?>/">
									<img src="<?=$collection['iconlarge']?>" width="179" height="134" alt="<?=$collection['title']?>" title="<?=$collection['title']?>" />
								</a>
							</div>
							<div class="info">

								<h2><a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/collections/<?=$collection['id']?>/">
									<?=$collection['title']?>
								</a></h2>

								<p class="count">
									<?=count($collection['set'])?> Sets
								</p>

							</div>
						</div>
					</td>
				<?php $i++;
				echo ($i % 3 == 0) ? '</tr><tr>' : ''; ?>
				<?php endforeach; ?>
			</tr>
		</table>	
	</div>
	
	<div id="photo_footer_bar" class="bar clearfix footer_bar">
		<?=Pager::getPager($fp->page, $page_count, $fp->canvas_url.'/photos/'.$fp->uid.'/collections/')?>
	</div>
	
<?php } ?>

<?=$fp->echo_footer(true)?>